/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProdutoVIEW.java
 *
 * Created on 17/10/2012, 10:00:19
 */

package view;

import dao.GrupoDAO;
import dao.ProdutoDAO;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import model.Grupo;
import model.Produto;

/**
 *
 * @author Ewerton
 */
public class ProdutoVIEW extends javax.swing.JDialog {

    /** Creates new form ProdutoVIEW */
    public ProdutoVIEW(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        ImageIcon icone = new ImageIcon("src/icons/produto.PNG");   
        setIconImage(icone.getImage());
        initComponents();
        capturaGrupo();
    }

    public void capturarDados(){
        Produto produto = new Produto();
        ProdutoDAO produtoDao = new ProdutoDAO();
        GrupoDAO grupoDao = new GrupoDAO();

       try{
            produto.setIdProduto(produtoDao.PegaUltimoRegistro());
            produto.setDescricao(tfDescrição.getText());
            produto.setUnidMedida((String)cbUnidade.getSelectedItem());
            produto.setCodigoBarras(tfCodigoBarras.getText());
            produto.setTipo((String) cbTipo.getSelectedItem());
            produto.setObservacao(tfObservacao.getText());
            produto.setCodGrupo(grupoDao.buscarPorDescricao((String)cbGrupo.getSelectedItem()).getIdGrupo());
            produtoDao.salvar(produto);  
            
            produto = produtoDao.buscarPorDescricao(tfDescrição.getText());
            tfIdProduto.setText(String.valueOf(produto.getIdProduto()));
       }catch(Exception ex){
           JOptionPane.showMessageDialog(null, ex);
       }
   }
    
   public void capturaGrupo(){
        GrupoDAO grupoDao = new GrupoDAO();
        Grupo grupo = new Grupo();
        Vector<Grupo> vector = grupoDao.listaTodos();
        int quant = vector.size();
        cbGrupo.removeAllItems();
        for(int i = 0; i < quant; i++){
            cbGrupo.addItem(vector.get(i).getDescricao());
        }
   }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPDadosGerais = new javax.swing.JPanel();
        jLDescricaoProduto = new javax.swing.JLabel();
        jLUnidMedida = new javax.swing.JLabel();
        tfDescrição = new javax.swing.JTextField();
        cbUnidade = new javax.swing.JComboBox();
        tfIdProduto = new javax.swing.JTextField();
        jLIDProduto = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        tfCodigoBarras = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        cbTipo = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        cbGrupo = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tfObservacao = new javax.swing.JTextArea();
        btIncluir = new javax.swing.JButton();
        jPBotoes = new javax.swing.JPanel();
        btConfirmar = new javax.swing.JButton();
        btCancelar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Produto");
        setResizable(false);

        jPDadosGerais.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados Gerais"));

        jLDescricaoProduto.setText("Descrição do Produto:");

        jLUnidMedida.setText("Unid. medida:");

        cbUnidade.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "UN", "KG", "LT", " " }));
        cbUnidade.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbUnidadeActionPerformed(evt);
            }
        });

        tfIdProduto.setEditable(false);
        tfIdProduto.setBackground(new java.awt.Color(204, 204, 255));
        tfIdProduto.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        tfIdProduto.setForeground(new java.awt.Color(153, 0, 0));
        tfIdProduto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfIdProdutoActionPerformed(evt);
            }
        });

        jLIDProduto.setText("ID:");

        jLabel1.setText("Código de Barras:");

        jLabel2.setText("Tipo:");

        cbTipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "MATERIAL DE CONSUMO", "BEM PERMANENTE (PATRIMÔNIO)", "SERVIÇO" }));

        jLabel3.setText("Grupo:");

        jLabel4.setText("Observações:");

        tfObservacao.setColumns(20);
        tfObservacao.setRows(5);
        jScrollPane1.setViewportView(tfObservacao);

        btIncluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/add24.png"))); // NOI18N
        btIncluir.setText("Incluir");
        btIncluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btIncluirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPDadosGeraisLayout = new javax.swing.GroupLayout(jPDadosGerais);
        jPDadosGerais.setLayout(jPDadosGeraisLayout);
        jPDadosGeraisLayout.setHorizontalGroup(
            jPDadosGeraisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPDadosGeraisLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPDadosGeraisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLUnidMedida)
                    .addComponent(jLDescricaoProduto)
                    .addComponent(jLabel1)
                    .addComponent(jLIDProduto))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPDadosGeraisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addComponent(tfIdProduto, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfDescrição)
                    .addComponent(tfCodigoBarras)
                    .addComponent(cbUnidade, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPDadosGeraisLayout.createSequentialGroup()
                        .addGroup(jPDadosGeraisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(cbGrupo, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(cbTipo, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btIncluir)))
                .addContainerGap())
        );
        jPDadosGeraisLayout.setVerticalGroup(
            jPDadosGeraisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPDadosGeraisLayout.createSequentialGroup()
                .addGroup(jPDadosGeraisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLIDProduto)
                    .addComponent(tfIdProduto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPDadosGeraisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tfCodigoBarras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPDadosGeraisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLDescricaoProduto)
                    .addComponent(tfDescrição, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPDadosGeraisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLUnidMedida)
                    .addComponent(cbUnidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPDadosGeraisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cbTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPDadosGeraisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cbGrupo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btIncluir))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPDadosGeraisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btConfirmar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/confirmar.png"))); // NOI18N
        btConfirmar.setText("Confirmar");
        btConfirmar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btConfirmarActionPerformed(evt);
            }
        });

        btCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/cancelar.png"))); // NOI18N
        btCancelar.setText("Cancelar");
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPBotoesLayout = new javax.swing.GroupLayout(jPBotoes);
        jPBotoes.setLayout(jPBotoesLayout);
        jPBotoesLayout.setHorizontalGroup(
            jPBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPBotoesLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btConfirmar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btCancelar)
                .addGap(89, 89, 89))
        );
        jPBotoesLayout.setVerticalGroup(
            jPBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPBotoesLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btConfirmar)
                    .addComponent(btCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.setBackground(new java.awt.Color(153, 153, 153));

        jLabel5.setFont(new java.awt.Font("Cooper Black", 0, 24)); // NOI18N
        jLabel5.setForeground(java.awt.Color.orange);
        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/produto64.png"))); // NOI18N
        jLabel5.setText("PRODUTO");
        jLabel5.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel5)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPDadosGerais, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPBotoes, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPDadosGerais, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPBotoes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    private void btConfirmarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btConfirmarActionPerformed
        if(tfCodigoBarras.getText().equals("") || tfDescrição.getText().equals("")){
            JOptionPane.showMessageDialog(null, "Favor preencha todos os campos!!!");
        }else{
            try{
                capturarDados();
                JOptionPane.showMessageDialog(null, "Produto cadastrado com sucesso!!!");
                this.dispose();
            }catch(Exception ex){
                System.out.println(ex.getMessage());
            }    
        }        
}//GEN-LAST:event_btConfirmarActionPerformed

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
        this.dispose();
}//GEN-LAST:event_btCancelarActionPerformed

    private void cbUnidadeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbUnidadeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbUnidadeActionPerformed

    private void tfIdProdutoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfIdProdutoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfIdProdutoActionPerformed

    private void btIncluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btIncluirActionPerformed
        GrupoView grupoView = new GrupoView(null, rootPaneCheckingEnabled);
        grupoView.setVisible(true);
        capturaGrupo();
    }//GEN-LAST:event_btIncluirActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ProdutoVIEW dialog = new ProdutoVIEW(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCancelar;
    private javax.swing.JButton btConfirmar;
    private javax.swing.JButton btIncluir;
    private javax.swing.JComboBox cbGrupo;
    private javax.swing.JComboBox cbTipo;
    private javax.swing.JComboBox cbUnidade;
    private javax.swing.JLabel jLDescricaoProduto;
    private javax.swing.JLabel jLIDProduto;
    private javax.swing.JLabel jLUnidMedida;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPBotoes;
    private javax.swing.JPanel jPDadosGerais;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField tfCodigoBarras;
    private javax.swing.JTextField tfDescrição;
    private javax.swing.JTextField tfIdProduto;
    private javax.swing.JTextArea tfObservacao;
    // End of variables declaration//GEN-END:variables

}
